package com.e1xp.deport_management_system.config;

import com.e1xp.deport_management_system.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author E1xP@foxmail.com
 * @version 1.0
 * @PACKAGE_NAME com.e1xp.deport_management_system.config
 * @CLASS_NAME MvcConfig
 * @Description TODO MVC配置类
 * @Date 2021/4/17 下午 12:13
 **/
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //映射静态文件
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /**
     * 添加拦截器，并排除静态资源与登陆访问
     * @param registry 拦截器注册器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**", "/user/login", "/user/preLogin", "/user/register", "/loginPage.html", "/registerPage.html");
    }

    /**
     * 添加视图
     * @param registry 视图控制器注册器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //将‘/’映射到主页
        registry.addViewController("/").setViewName("forward:/index/frontData");
    }
}
